package com.zlq.mail;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.internet.MimeMessage;
import java.io.File;

@Service
public class MailService3 {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private JavaMailSender mailSender;
    @Value("${mail.fromMail.addr}")
    private String from;
    public void sendSimpleMail(String to, String subject, String content, String filePath) {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        try {
            MimeMessageHelper message = new MimeMessageHelper(mimeMessage,true);
            message.setFrom(from);
            message.setTo(to);
            message.setSubject(subject);
            message.setText(content,true);
            //FileSystemResource文件系统资源
            FileSystemResource resource = new FileSystemResource(new File(filePath));
            String s = filePath.substring(filePath.lastIndexOf(File.separator));
            message.addAttachment(s,resource);
            mailSender.send(mimeMessage);
            logger.info("html邮件已经发送。");
        } catch (Exception e) {
            logger.error("发送html邮件时发生异常！", e);
        }
    }
}

